package com.jming.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jming.yygh.hosp.repository.DepartmentRepository;
import com.jming.yygh.hosp.service.DepartmentService;
import com.jming.yygh.model.hosp.Department;
import com.jming.yygh.vo.hosp.DepartmentQueryVo;
import com.jming.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jming
 * @date 2021-04-23
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {

        // 1、把map集合转换成科室对象(先转换成字符串，在转换成Department对象)
        String mapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(mapString, Department.class);

        // 2、判断是否存在科室信息
        Department departmentExist = departmentRepository
                .getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExist != null) {
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {

        // 创建pageable对象，设置当前页和每页记录数
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        // 调用查询方法并返回结果
        return departmentRepository.findAll(example, pageable);
    }

    @Override
    public void removo(String hoscode, String depcode) {

        // 根据医院编号和科室编号查询科室信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        // 判断department是否存在
        if (department != null) {
            // 调用删除方法
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {

        // 创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        // 根据医院编号，查询所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);

        // 所有科室的列表信息
        List<Department> departmentList = departmentRepository.findAll(example);

        // 根据大科室编号bigcode分组，获取每个大科室里面下级科室
        Map<String, List<Department>>
                departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        // 遍历departmentMap集合
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            // 大科室编号
            String bigCode = entry.getKey();

            // 大科室编号对应的小科室集合
            List<Department> departmentMinList = entry.getValue();

            // 封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigCode);
            departmentVo1.setDepname(departmentMinList.get(0).getBigname());

            // 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : departmentList) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                // 封装到list里面
                children.add(departmentVo2);
            }

            // 把小科室集合放到children里面
            departmentVo1.setChildren(children);
            // 放到最终result里面
            result.add(departmentVo1);
        }

        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode).getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {

        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
